// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-12-31

#include "stdafx.h"
#include "dbmsg_base.h"
#include "svr_server.h"
#include "svr_asyncobj.h"
#include "db_utility.h"
#include "svr_showinfo.h"

namespace JoyServer
{
;
class CJoyAsyncSendDbMsg : public CJoyAsyncObject
{
public:
    CJoyServer* m_server;
    UINT    m_msgServerId;
    UINT    m_msgType;
    CString m_msgContent;
public:
    CJoyAsyncSendDbMsg(CJoyServer* pServer):
        m_server(pServer)
    {
        m_msgServerId   = 0;
        m_msgType       = 0;
    }
    virtual void OnExecute();
};

void CJoyAsyncSendDbMsg::OnExecute()
{
    // open online server database session.
    CJoyDbSource& dbOServ = m_server->m_dbSetOServ.GetSource(m_msgServerId);
    CJoyDbSession sessOServ;
    if(!JoyOpenDbSession(m_server, sessOServ, dbOServ))
    {
        return;
    }
    static PCWSTR cmd64   = L"battle_send_message64";
    static PCWSTR cmd256  = L"battle_send_message256";
    static PCWSTR cmd1024 = L"battle_send_message1024";
    static PCWSTR cmd4096 = L"battle_send_message4096";

    PCWSTR cmdText = NULL;
    if(m_msgContent.GetLength() < 64)
    {
        cmdText = cmd64;
    }
    else if(m_msgContent.GetLength() < 256)
    {
        cmdText = cmd256;
    }
    else if(m_msgContent.GetLength() < 1024)
    {
        cmdText = cmd1024;
    }
    else if(m_msgContent.GetLength() < 4096)
    {
        cmdText = cmd4096;
    }
    else
    {
        CString content(m_msgContent, 128);
        m_server->m_showInfo->QueueTimeColorText(
            InfoColor_Error,
            "bad dbmessage content length.\n"
            "  message type : %d.\n"
            "  server id : %d.\n"
            "  content text : %s.\n",
            m_msgType,
            m_msgServerId,
            (PCSTR)content);
        return;
    }

    CJoyDbCommand sendCmd;
    if(!JoyInvokeCommand(m_server, sessOServ, sendCmd, 
        cmdText, 
        m_msgServerId,
        m_msgType,
        m_msgContent))
    {
        return;
    }
}

// class CJoyDbMsgHandler

CJoyDbMsgHandler::CJoyDbMsgHandler()
{
    m_server = NULL;
}

CJoyDbMsgHandler::~CJoyDbMsgHandler()
{
}

bool CJoyDbMsgHandler::Create(CJoyServer* pServer)
{
    m_server = pServer;
    return true;
}

void CJoyDbMsgHandler::Destroy()
{
}

void CJoyDbMsgHandler::DbMsg_Recv(const CJoyDbMsg& msg)
{
    switch(msg.m_type)
    {
#   define JOY_DBMSG_DEF(name, id) case DBMSG_##name: OnDbMsg_##name(msg); break;
#   include "dbmsg_msgdef.h"
    }
}

void CJoyDbMsgHandler::DbMsg_Rowset(CJoyDbCommand& row)
{
    CJoyDbMsg msg;
    row.GetValue("type", msg.m_type);
    row.GetValue("content", msg.m_content);
    msg.m_root  = JoyXmlParse(msg.m_content, msg.m_content.GetLength());
    DbMsg_Recv(msg);
}

void CJoyDbMsgHandler::SendMsg(UINT serverId, UINT msgType, const CString& strContent)
{
    CJoyAsyncSendDbMsg* sendDbMsg = new CJoyAsyncSendDbMsg(m_server);
    sendDbMsg->m_msgServerId= serverId;
    sendDbMsg->m_msgType    = msgType;
    sendDbMsg->m_msgContent = strContent;
    m_server->QueueAsyncObject(sendDbMsg);
}

bool CJoyDbMsgHandler::GetUserServer(UINT userId, UINT& loginServer, UINT& tempLoginId, UINT& mainServer, UINT& tempMainId)
{
    loginServer = 0;
    mainServer  = 0;
    tempLoginId = 0;
    tempMainId  = 0;

    // open online userdb session.
    CJoyDbSource& dbOUser = m_server->m_dbSetOUser.GetSource(userId);
    CJoyDbSession sessOUser;
    if(!JoyOpenDbSession(m_server, sessOUser, dbOUser))
    {
        return false;
    }

    // get the server ids.
    CJoyDbCommand getCmd;
    if(!JoyInvokeCommand(m_server, sessOUser, getCmd, 
        L"battle_get_user_server", 
        userId))
    {
        return false;
    }

    if(!HasMoreRow(getCmd.MoveNext()))
    {
        return false;
    }

    getCmd.GetValue("login_server", loginServer);
    getCmd.GetValue("main_server", mainServer);
    getCmd.GetValue("temp_login_id", tempLoginId);
    getCmd.GetValue("temp_main_id", tempMainId);

    return true;
}

} // namespace JoyServer